package com.ymatou.promotion.api.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.promotion.api.parameter.AvailableCouponInfo;
import com.ymatou.promotion.api.parameter.ProductInfo;
import com.ymatou.promotion.api.parameter.ValidateCouponListBean;
import com.ymatou.promotion.api.service.ValidateCouponListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.YmtCoupon;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponScenario;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.model.YmtCouponValue;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponScenarioWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponValueWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 获取有效的优惠券列表接口
 *
 * @author chenjiazhu
 *
 *         备注：根据产品搜索的需要等商品整合完后添加
 *
 *         逻辑： 1. 先根据买家ID(BuyerId)取当前用户可以使用的有效优惠券列表 SP_CouponValidList,@BuyerUserId
 *         -- ============================================= -- Author:
 *         zhujinfeng -- Create date: 2015-02-25 -- Description:
 *         取有效的优惠券列表（使用场景：订单生成页面） --
 *         ============================================= CREATE PROCEDURE
 *         [dbo].[SP_CouponValidList]
 * @BuyerUserId INT AS BEGIN
 * 
 *              SET NOCOUNT ON;
 * 
 *              SELECT a.sCouponCode, b.sBatchCode, a.iCouponType,
 *              dbo.FUNC_MargeCouponValue(a.iCouponSetting) AS CouponValue,
 *              c.iCouponUseType, c.iMaxUseTime, a.dValidStart, a.dValidEnd,
 *              f.iCouponUsedCount, e.iUserType, e.sSellerIds,
 *              e.sLogisticsTypes, e.sStockStatus, e.sProductCategories,
 *              e.sSpecificProducts, e.sProductBrands, e.sActivityIds,
 *              e.sUserLevels,
 *              e.sUsePlatforms,C.bIsMobileVerify,b.iBatchCreateType FROM
 *              Ymt_Coupon a WITH(NOLOCK) LEFT JOIN Ymt_CouponBatch b
 *              WITH(NOLOCK) ON a.iBatchId = b.iBatchId LEFT JOIN
 *              Ymt_CouponSetting c WITH(NOLOCK) ON a.iCouponSetting =
 *              c.iCouponSettingId LEFT JOIN Ymt_CouponScenario e WITH(NOLOCK)
 *              ON c.iScenarioId = e.iCouponScenarioId LEFT JOIN
 *              Ymt_CouponPrivateUserBound f WITH(NOLOCK) ON a.sCouponCode =
 *              f.sCouponCode WHERE f.iUserId = @BuyerUserId AND (GETDATE()
 *              BETWEEN a.dValidStart AND a.dValidEnd) AND f.iCouponUsedCount >
 *              0 AND c.iMaxUseTime > 0 END
 *
 *              2. 遍历1中取的有效优惠券列表，根据请求中参数筛选 if (scenario == null) { messageText =
 *              "券码不存在"; return false; }
 *
 *              if (scenario.bIsMobileVerify &&
 *              string.IsNullOrWhiteSpace(userLoginMobileAccount)) { messageText
 *              = "券码仅通过手机验证的用户可以使用"; return false; }
 *
 *              // 检查优惠券的有效期 if (scenario.dValidStart.Date > DateTime.Today ||
 *              scenario.dValidEnd.Date < DateTime.Today) { messageText =
 *              "券码不在有效期内"; return false; }
 *
 *              // 检查优惠券可用次数 if (scenario.iCouponType == 1) { if
 *              (!ValidCoupon.PublicCouponIsCanUser(scenario.sCouponCode,
 *              buyerUserId)) { messageText = "券码使用次数已超过最大使用数"; return false; }
 *              } else { if (scenario.iMaxUseTime <= 0 ||
 *              scenario.iCouponUsedCount <= 0) { messageText =
 *              "券码使用次数已超过最大使用数"; return false; } }
 *
 *              // 检查优惠券适用会员类型 - 新用户 if (buyerOrderNum == 0 &&
 *              scenario.iUserType != 1 && scenario.iUserType != 0) {
 *              messageText = "当前用户不能使用本券码"; return false; }
 *
 *              // 检查优惠券适用会员类型 - 老用户 if (buyerOrderNum > 0 && scenario.iUserType
 *              == 1) { messageText = "当前用户不能使用本券码"; return false; }
 *
 *              // 检查优惠券的适用平台 if (!string.IsNullOrEmpty(scenario.sUsePlatforms)
 *              && !scenario.sUsePlatforms.Split(new[] { "," },
 *              StringSplitOptions.RemoveEmptyEntries)
 *              .Contains(platformType.ToString(CultureInfo.InvariantCulture)))
 *              { messageText = "当前平台不能使用本券码"; return false; }
 *
 *              // 检查优惠券适用用户等级 if (!string.IsNullOrEmpty(scenario.sUserLevels)
 *              && !scenario.sUserLevels.Split(new[] {","},
 *              StringSplitOptions.RemoveEmptyEntries)
 *              .Contains(userLevel.ToString(CultureInfo.InvariantCulture))) {
 *              messageText = "当前用户等级不能使用本券码"; return false; }
 *
 *              // 检查优惠券适用卖家 if (!string.IsNullOrEmpty(scenario.sSellerIds) &&
 *              !scenario.sSellerIds.Split(new[] {","},
 *              StringSplitOptions.RemoveEmptyEntries)
 *              .Contains(sellerId.ToString(CultureInfo.InvariantCulture))) {
 *              messageText = "券码指定卖家，与当前商品不匹配"; return false; }
 *
 *              // 检查优惠券适用商品活动 if
 *              (!string.IsNullOrWhiteSpace(scenario.sActivityIds)) { if
 *              (!CheckProductActivity(scenario.sActivityIds, productList)) {
 *              messageText = "券码指定活动，与当前商品不匹配"; return false; } } else {
 *              //活动商品，不可使用优惠券的金额 var unableUseCouponAmount =
 *              productList.Where(p => p.ActivityId > 0 && p.IsActivityProduct
 *              && !p.CouponAvail4OrderDeduct) .Sum(p =>
 *              p.ProductQty*p.ProductPrice); orderAmount -=
 *              unableUseCouponAmount; }
 *
 *              // 检查优惠券适用商品物流状态 if
 *              (!CheckProductLogistics(scenario.sLogisticsTypes, productList))
 *              { messageText = "券码指定配送，与当前商品不匹配"; return false; }
 *
 *              // 检查优惠券适用商品分类 if
 *              (!CheckProductCategories(scenario.sProductCategories,
 *              productList)) { messageText = "券码指定类目，与当前商品不匹配"; return false; }
 *
 *              // 检查优惠券适用品牌 if (!CheckProductBrands(scenario.sProductBrands,
 *              productList)) { messageText = "券码指定品牌，与当前商品不匹配"; return false; }
 *
 *              // 检查优惠券适用特殊商品 if
 *              (!CheckSpecificProducts(scenario.sSpecificProducts,
 *              productList)) { messageText = "券码指定商品，与当前商品不匹配"; return false; }
 *
 *              Dictionary<string, Dictionary<string, decimal>> useCondition =
 *              GroupByUseCondition(scenario.sLogisticsTypes,
 *              scenario.sProductCategories, scenario.sProductBrands,
 *              scenario.sSpecificProducts, scenario.sActivityIds, productList);
 *              if (useCondition != null && useCondition.Count > 0) { if
 *              (!string.IsNullOrWhiteSpace(scenario.sActivityIds)) {
 *              orderAmount = useCondition["activity"].Sum(v => v.Value); } else
 *              { if (!string.IsNullOrWhiteSpace(scenario.sProductCategories)) {
 *              orderAmount = useCondition["category"].Sum(v => v.Value); } if
 *              (!string.IsNullOrWhiteSpace(scenario.sProductBrands)) {
 *              orderAmount = useCondition["brand"].Sum(v => v.Value); } if
 *              (!string.IsNullOrWhiteSpace(scenario.sSpecificProducts)) {
 *              orderAmount = useCondition["product"].Sum(v => v.Value); } } }
 *              ApplicationLog.Info(string.Format("验证结果：{0}", messageText));
 *              decimal minOrderAmount; decimal couponAmount =
 *              CouponValue.GetCouponAmount(scenario.CouponValue, orderAmount,
 *              out minOrderAmount); if (couponAmount == 0m) { messageText =
 *              "金额与优惠券使用条件不匹配"; return false; }
 */
@TestSuite
public class Ts_ValidateCouponList {
    private static ValidateCouponListBean validatecouponlistBean;
    private static ValidateCouponListCall validatecouponlistCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("获取有效的优惠券列表接口");
    }

    @Before
    public void caseUp() {
        validatecouponlistBean = new ValidateCouponListBean();
        validatecouponlistCall = new ValidateCouponListCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(validatecouponlistCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_001() {
        Logger.start(true, "获取有效的优惠券列表-按平台类型=1：洋码头PC（M商品在M2C-PC成交）搜索");
        try {
            setDefaultValidateRecieveCouponListBean(1);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_002() {
        Logger.start(true, "获取有效的优惠券列表-按平台类型=2：洋码头WAP（M商品在M2C-WAP成交）搜索");
        try {

            setDefaultValidateRecieveCouponListBean(2);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_003() {
        Logger.start(true, "获取有效的优惠券列表-按平台类型=3：洋码头APP（M商品在M2C-APP成交）搜索");
        try {

            setDefaultValidateRecieveCouponListBean(3);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_004() {
        Logger.start(true, "获取有效的优惠券列表-按平台类型=4：神器APP（C商品在C2C-APP成交）搜索");
        try {

            setDefaultValidateRecieveCouponListBean(4);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_005() {
        Logger.start(true, "获取有效的优惠券列表-按平台类型=5：神器WAP（C商品在C2C-WAP成交））搜索");
        try {

            setDefaultValidateRecieveCouponListBean(5);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_006() {
        Logger.start(true, "获取有效的优惠券列表-按平台类型=6：神器PC（C商品在C2C-PC成交）搜索");
        try {

            setDefaultValidateRecieveCouponListBean(6);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_007() {
        Logger.start(true, "获取有效的优惠券列表-按平台类型=7：神器微信（C商品通过C2C分享到微信成交）搜索");
        try {

            setDefaultValidateRecieveCouponListBean(7);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_008() {
        Logger.start(true, "获取有效的优惠券列表-按平台类型=8：M商品神器APP（M商品在C2C-APP成交）搜索");
        try {

            setDefaultValidateRecieveCouponListBean(8);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_009() {
        Logger.start(true, "获取有效的优惠券列表-按平台类型=9：M商品神器WAP（M商品在C2C-WAP成交）搜索");
        try {

            setDefaultValidateRecieveCouponListBean(9);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_010() {
        Logger.start(true, "获取有效的优惠券列表-按平台类型=10：M商品神器微信（M商品在C2C分享到微信成交）搜索");
        try {

            setDefaultValidateRecieveCouponListBean(10);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_011() {
        Logger.start(true, "获取有效的优惠券列表-按平台类型=11：M商品微信（M商品在其它产品分享到微信成交）搜索");
        try {

            setDefaultValidateRecieveCouponListBean(11);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_012() {
        Logger.start(true, "获取有效的优惠券列表-按平台类型=12：下单器PC（在PC使用下单器生成订单）搜索");
        try {

            setDefaultValidateRecieveCouponListBean(12);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_013() {
        Logger.start(true, "获取有效的优惠券列表-按平台类型=13：M商品其他渠道下单搜索");
        try {

            setDefaultValidateRecieveCouponListBean(13);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_014() {
        Logger.start(true, "获取有效的优惠券列表-按平台类型=14：C商品其他渠道下单）搜索");
        try {

            setDefaultValidateRecieveCouponListBean(14);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_015() {
        Logger.start(true, "获取有效的优惠券列表-按用户等级=0：Y0搜索");
        try {

            setDefaultValidateRecieveCouponListBean(0);
            validatecouponlistBean.setUserLevel(0);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_016() {
        Logger.start(true, "获取有效的优惠券列表-按用户等级=1：Y1搜索");
        try {

            setDefaultValidateRecieveCouponListBean(1);
            validatecouponlistBean.setUserLevel(0);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_017() {
        Logger.start(true, "获取有效的优惠券列表-按用户等级=2：Y2搜索");
        try {

            setDefaultValidateRecieveCouponListBean(2);
            validatecouponlistBean.setUserLevel(0);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_018() {
        Logger.start(true, "获取有效的优惠券列表-按用户等级=3：Y3搜索");
        try {

            setDefaultValidateRecieveCouponListBean(3);
            validatecouponlistBean.setUserLevel(0);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_019() {
        Logger.start(true, "获取有效的优惠券列表-按用户等级=4：Y4搜索");
        try {

            setDefaultValidateRecieveCouponListBean(4);
            validatecouponlistBean.setUserLevel(0);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_020() {
        Logger.start(true, "获取有效的优惠券列表-按用户等级=5：Y5搜索");
        try {

            setDefaultValidateRecieveCouponListBean(5);
            validatecouponlistBean.setUserLevel(0);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_021() {
        Logger.start(true, "获取有效的优惠券列表-按用户等级=6：Y6搜索");
        try {

            setDefaultValidateRecieveCouponListBean(6);
            validatecouponlistBean.setUserLevel(0);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_022() {
        Logger.start(true, "获取有效的优惠券列表-按用户绑定手机号UserLoginMobileAccount搜索");
        try {

            setDefaultValidateRecieveCouponListBean(1);
            validatecouponlistBean.setUserLoginMobileAccount("18611111111");

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            verifyReturn(1);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_023() {
        Logger.start(false, "反例-场景：OrderAmount空");
        try {
            setDefaultValidateRecieveCouponListBean(1);
            validatecouponlistBean.setOrderAmount(null);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            Logger.verifyEquals(ECode.SUCCESS,
                    validatecouponlistCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0,
                    validatecouponlistCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals(ECode.COUPON_ValidateRecieveCouponList_Fail1,
                    validatecouponlistCall.getMessageText(), "验证报错信息");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_024() {
        Logger.start(false, "反例-场景：BuyerId空");
        try {
            setDefaultValidateRecieveCouponListBean(1);
            validatecouponlistBean.setBuyerId(null);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            Logger.verifyEquals(ECode.SUCCESS,
                    validatecouponlistCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0,
                    validatecouponlistCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals(ECode.COUPON_ValidateRecieveCouponList_Fail1,
                    validatecouponlistCall.getMessageText(), "验证报错信息");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_025() {
        Logger.start(false, "反例-场景：SellerId空");
        try {
            setDefaultValidateRecieveCouponListBean(1);
            validatecouponlistBean.setSellerId(null);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            Logger.verifyEquals(ECode.SUCCESS,
                    validatecouponlistCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0,
                    validatecouponlistCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals(ECode.COUPON_ValidateRecieveCouponList_Fail1,
                    validatecouponlistCall.getMessageText(), "验证报错信息");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_026() {
        Logger.start(false, "反例-场景：PlatformType空");
        try {
            setDefaultValidateRecieveCouponListBean(1);
            validatecouponlistBean.setPlatformType(null);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            Logger.verifyEquals(ECode.SUCCESS,
                    validatecouponlistCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0,
                    validatecouponlistCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals(true, validatecouponlistCall.getMessageText()
                    .contains("个可用的优惠券"), "验证返回信息（传空时取所有平台优惠券）");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_027() {
        Logger.start(false, "反例-场景：UserLevel空");
        try {
            setDefaultValidateRecieveCouponListBean(1);
            validatecouponlistBean.setUserLevel(null);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            Logger.verifyEquals(ECode.SUCCESS,
                    validatecouponlistCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0,
                    validatecouponlistCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals(true, validatecouponlistCall.getMessageText()
                    .contains("个可用的优惠券"), "验证返回信息（传空时默认取0）");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ValidateCouponList_028() {
        Logger.start(false, "反例-场景：BuyerOrderNum空");
        try {
            setDefaultValidateRecieveCouponListBean(1);
            validatecouponlistBean.setBuyerOrderNum(null);

            validatecouponlistCall.setData(validatecouponlistBean);
            validatecouponlistCall.callService();

            Logger.verifyEquals(ECode.SUCCESS,
                    validatecouponlistCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0,
                    validatecouponlistCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals(true, validatecouponlistCall.getMessageText()
                    .contains("个可用的优惠券"), "验证返回信息（传空时默认取0）");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    /**
     * 设置 默认 已领取 全场 优惠券bean
     * 
     * @param scenarioType
     * @param usePlatforms
     * @throws ClientProtocolException
     * @throws IOException
     */
    private void setDefaultValidateRecieveCouponListBean(Integer platformType)
            throws ClientProtocolException, IOException {
        int buyerId = Integer.parseInt(EnvSetup.getData("BuyUserId"));
        int sellerId = Integer.parseInt(EnvSetup.getData("SellerId"));

        // setBean
        validatecouponlistBean.setOrderAmount(new BigDecimal(2));
        validatecouponlistBean.setBuyerId(buyerId);
        validatecouponlistBean.setSellerId(sellerId);

        validatecouponlistBean.setPlatformType(platformType);

        validatecouponlistBean.setBuyerOrderNum(0);
        validatecouponlistBean.setUserLevel(0);

        List<ProductInfo> productInfoList = new ArrayList<ProductInfo>();
        validatecouponlistBean.setProductList(productInfoList);
    }

    private void verifyReturn(int CouponState) {
        Logger.verifyEquals(ECode.SUCCESS, validatecouponlistCall.getCode(),
                "验证返回code");
        Logger.verifyEquals(ECode.BCODE0, validatecouponlistCall.getBCode(),
                "验证返回Bcode");
        Logger.verifyEquals(true, validatecouponlistCall.getMessageText()
                .contains("可用的优惠券"), "检查返回信息");

        List<AvailableCouponInfo> availableCouponList = validatecouponlistCall
                .getAvailableCouponList().getAvailableCouponList();

        int i = 1;

        for (AvailableCouponInfo availableCouponInfo : availableCouponList) {

            Logger.comment("验证第" + i + "条记录");
            i++;

            YmtCoupon coupon = new YmtCouponWapper().selectBysCouponCode(
                    availableCouponInfo.getCouponCode()).get(0);
            YmtCouponPrivateUserBound couponPrivateUserBound = new YmtCouponPrivateUserBoundWapper()
                    .selectBysCouponCode(availableCouponInfo.getCouponCode())
                    .get(0);
            YmtCouponBatch couponBatch = new YmtCouponBatchWapper()
                    .selectByPrimaryKey(coupon.getiBatchId());
            YmtCouponValue couponValue = new YmtCouponValueWapper()
                    .selectByiCouponSettingId(couponBatch.getiCouponSettingId())
                    .get(0);
            YmtCouponSetting couponSetting = new YmtCouponSettingWapper()
                    .selectByiCouponSettingId(couponBatch.getiCouponSettingId())
                    .get(0);
            YmtCouponScenario couponScenario = new YmtCouponScenarioWapper()
                    .selectByiCouponScenarioId(couponSetting.getiScenarioId())
                    .get(0);

            // 验证返回 BatchCode
            Logger.verifyEquals(couponBatch.getsBatchCode(),
                    availableCouponInfo.getBatchCode(),
                    "验证BatchCode与数据库表ymt_couponBatch中sBatchCode一致");

            // 验证返回 MinOrderAmount
            Logger.verifyEquals(0, availableCouponInfo.getMinOrderAmount()
                    .compareTo(couponValue.getFminordervalue()),
                    "验证返回MinOrderAmount与数据库表ymt_couponValue中Fminordervalue一致");

            // 验证返回 CouponValue
            Logger.verifyEquals(0, availableCouponInfo.getCouponValue()
                    .compareTo(couponValue.getFcouponvalue()),
                    "验证返回CouponValue与数据库表ymt_couponValue中Fcouponvalue中一致");

            // 验证返回 CouponType
            Logger.verifyEquals(2, availableCouponInfo.getCouponType(),
                    "验证返回CouponType為2");

            // 验证返回 CouponUseType
            Logger.verifyEquals(couponSetting.getiCouponUseType(),
                    Integer.parseInt(availableCouponInfo.getCouponUseType()),
                    "验证返回CouponUseType与数据库中一致");

            // // 验证返回 PlatformType与数据库一致
            // if(availableCouponInfo.getPlatformType()!=0 ||
            // couponScenario.getSuseplatforms()!=null)
            // {
            // Logger.verifyEquals(couponScenario.getSuseplatforms(),
            // availableCouponInfo.getPlatformType(),
            // "验证返回PlatformType与数据库中一致,接口没有返回数据，需要修复");
            // }

            SimpleDateFormat formatter = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");

            // 验证返回 ValidBegin
            Logger.verifyEquals(getDate(coupon.getdValidStart()).substring(0, 18),
                    availableCouponInfo.getValidBegin().substring(0, 18),
                    "验证返回ValidBegin与数据库表ymt_coupon字段dValidStart中一致");

            // 验证返回 ValidEnd
            Logger.verifyEquals(getDate(coupon.getdValidEnd()),
                    availableCouponInfo.getValidEnd(),
                    "验证返回ValidEnd与数据库表ymt_coupon字段dValidEnd中一致");

            // 验证返回 Remainder
            Logger.verifyEquals(1, availableCouponInfo.getRemainder(),
                    "验证返回Remainder正确");

            // 验证返回 CouponState
            Logger.verifyEquals(CouponState,
                    availableCouponInfo.getCouponState(), "验证返回CouponState正确");

            // // 验证返回 UserId
            // Logger.verifyEquals(buyerId,
            // availableCouponInfo.getUserId(),
            // "验证返回UserId与数据库中一致,接口没有返回数据，需要修复");
            //
            // // 验证返回 UserName
            // Logger.verifyEquals(EnvSetup.getData("BuyUserName"),
            // availableCouponInfo.getUserName(),
            // "验证返回UserName与数据库中一致,接口没有返回数据，需要修复");

            // 验证返回 CreateType
            Logger.verifyEquals(2, availableCouponInfo.getCreateType(),
                    "验证返回CreateType正确");

            // 验证useplatforms筛选正确
            if (couponScenario.getSuseplatforms() != null
                    && validatecouponlistBean.getPlatformType() != 0)
                Logger.verifyEquals(validatecouponlistBean.getPlatformType()
                        .toString(), couponScenario.getSuseplatforms(),
                        "验证PlatformType筛选正确");

            // 验证UserLevel筛选正确
            if (couponScenario.getSuserlevels() != null
                    && validatecouponlistBean.getUserLevel() != 0) {
                Logger.verifyEquals(validatecouponlistBean.getUserLevel()
                        .toString(), couponScenario.getSuserlevels(),
                        "验证userlevels筛选正确");
            }

            // 验证UserLoginMobileAccount筛选正确
            // if (validatecouponlistBean.getUserLoginMobileAccount() != null
            // && !validatecouponlistBean.getUserLoginMobileAccount().equals("")) {
            // Logger.verifyEquals(1, couponSetting.getbIsMobileVerify(),
            // "验证UserLoginMobileAccount筛选正确");
            // }

            // 验证ProductList相关
        }
    }

    private String getDate(Date date) {
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");

        return formatter1.format(date) + "T" + formatter2.format(date);
    }
}
